package cn.autumnorange.commons.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;

import java.io.IOException;
import java.util.Date;
import java.util.HashSet;

public class TokenUtil {

    public static OAuth2AccessToken getToken(String authorizationUrl, String authorization, String code, String redirect_uri,
                                             String state) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = authorizationUrl +
                "?grant_type=authorization_code"
                + "&code="
                + code
                + "&redirect_uri="
                + redirect_uri;
        if (state != null) {
            url += "&state" + state;
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Authorization", authorization);

        HttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {

            e.printStackTrace();
        } catch (IOException e) {
           
            e.printStackTrace();
        }
        String result = "";
        if (response.getStatusLine().getStatusCode() == 200) {
            try {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
                // 解析token的json数据
                JSONObject jsonObject = JSONObject.parseObject(result);
                DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(jsonObject.getString("access_token"));
//        accessToken.setAccess_token();
                accessToken.setTokenType(jsonObject.getString("token_type"));
//        accessToken.setToken_type(jsonObject.getString("token_type"));
                accessToken.setRefreshToken(new DefaultOAuth2RefreshToken(jsonObject.getString("refresh_token")));
//        accessToken.setRefresh_token(jsonObject.getString("refresh_token"));
//        accessToken.setExpires_in(jsonObject.getString("expires_in"));
                accessToken.setExpiration(new Date(Long.parseLong(jsonObject.getString("expires_in"))));
                HashSet set = new HashSet();
                set.add(jsonObject.getString("scope"));
                accessToken.setScope(set);
                return accessToken;
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
}
